﻿hpxAdminApp.factory('orderService', ['Restangular', function (Restangular) {
    var res = Restangular.all('orders');
    return {
        get: function (id) {
            return res.one(id).get();
        },

        getOne: function (id) {
            return res.one('order',id).get();
        },

        query: function (params, filter) {
            var queryParam = {
                'status':filter.status,
                'orderPayTypeId':filter.orderPayTypeId,
                'func':filter.func,
                'p': params.page(),
                'n': params.count(),
                'orderBy': params.orderBy()
            }
            return res.get('order', queryParam).then(function (result) {
                params.total(result.page_info.items_number);
                return result.orders;
            });
        },

        getAllAliveOrders: function (params, filter) {
            var queryParam = {
                'func': filter.func,
                'orderStatus':filter.orderStatus,
                'tradeTypeCode':filter.tradeTypeCode,
                'enterpriseName':filter.enterpriseName,
                'personPhone':filter.personPhone,
                'deadlineTime1':filter.deadlineTime1,
                'deadlineTime2':filter.deadlineTime2,
                'orderPayType':filter.orderPayType,
                'customerName': filter.customerName,

                'p': params.page(),
                'n': params.count(),
                'orderBy': params.orderBy()
            }
            return res.get('order', queryParam).then(function (result) {
                params.total(result.page_info.items_number);
                return result.orders;
            });
        },

        getAllOrders: function (params, filter) {
            var queryParam = {
                'func': filter.func,
                'order_number': filter.order_number,
                'status': filter.status,
                'p': params.page(),
                'n': params.count(),
                'orderBy': params.orderBy()
            }
            return res.get('order' + '/user' , queryParam).then(function (result) {
                params.total(result.page_info.items_number);
                return result.listName;
            });
        },

        stopOrder: function (model) {
            return res.one('orderStop', model.id).customPUT(model);
        },
        getOrderCount:function () {
            return res.get('orderCount');
        },
        updateOrderCount:function (model) {
            return res.one('count').customPUT(model);
        }

    };
}]);